﻿using System.Linq;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Colors;
using System.Collections.Generic;

namespace BasicTools
{
    /// <summary>
    /// 显示、隐藏、隔离（配套图层控制工具类）
    /// </summary>
    public enum DisplayOrHide
    {
        Display = 0,
        Hide = 1,
        Isolate = 2
    }

    /// <summary>
    /// 图层控制工具类
    /// </summary>
    public static class LayerControlTools
    {
        private static readonly Document _doc = Application.DocumentManager.MdiActiveDocument;
        private static readonly Database _db = _doc.Database;
        private static readonly Editor _ed = _doc.Editor;

        /// <summary>
        /// 显示或隐藏当前模型空间中指定颜色的所有实体
        /// </summary>
        /// <typeparam name="T">T:Entity</typeparam>
        /// <param name="color">颜色</param>
        /// <param name="displayOrHide">显示或隐藏</param>
        public static void DisplayOrHideEntitiesByColor<T>(Color color, DisplayOrHide displayOrHide) where T : Entity
        {
            using (var trans = _db.TransactionManager.StartTransaction())
            {
                try
                {
                    var entities = (from entity in _db.GetEntsInModelSpace<T>()
                                    where (entity.Color == color && entity.Visible == true)
                                    select entity).ToList();
                    _ed.WriteMessage(entities.Count.ToString());

                    var entityYesIds = (from entity in entities
                                        where entity.Color == color
                                        select entity.Id).ToList();
                    _ed.WriteMessage(entityYesIds.Count.ToString());

                    var entityNotIds = (from entity in entities
                                        where entity.Color != color
                                        select entity.Id).ToList();
                    _ed.WriteMessage(entityYesIds.Count.ToString());

                    switch (displayOrHide)
                    {
                        case DisplayOrHide.Display:
                            //未实现
                            break;
                        case DisplayOrHide.Hide:
                            foreach (var id in entityYesIds)
                                (trans.GetObject(id, OpenMode.ForWrite) as Entity).Visible = false;
                            break;
                        case DisplayOrHide.Isolate:

                            foreach (var id in entityNotIds)
                                (trans.GetObject(id, OpenMode.ForWrite) as Entity).Visible = false;
                            break;
                        default:
                            break;
                    }

                    trans.Commit();
                }
                catch (System.Exception ex)
                {
                    Application.ShowAlertDialog(ex.ToString());
                    trans.Abort();
                }
            }
        }//还需要测试和修改，有点问题

        /// <summary>
        /// 显示或隐藏当前模型空间中指定颜色的所有实体
        /// </summary>
        /// <param name="color">颜色</param>
        /// <param name="displayOrHide">显示或隐藏</param>
        public static void DisplayOrHideEntitiesByColor(Color color, ObjectId layerId, DisplayOrHide displayOrHide)
        {
            using (var trans = _db.TransactionManager.StartTransaction())
            {
                try
                {
                    if (color.IsByLayer)
                    {
                        LayerTable lt = trans.GetObject(_db.LayerTableId, OpenMode.ForRead) as LayerTable;
                        LayerTableRecord ltr = trans.GetObject(layerId, OpenMode.ForRead) as LayerTableRecord;
                        color = ltr.Color;
                    }

                    var layers = (from layer in _db.GetAllLayers()
                                  where layer.Color == color
                                  select layer);
                    var layerIds = (from layer in layers
                                    select layer.Id).ToList();

                    var entities = from entity in _db.GetEntsInModelSpace()
                                   where (entity.Visible == true)
                                   select entity;
                    var entityIds = from entity in entities
                                    select entity.Id;
                    
                    var entityYesIds = from entity in entities
                                       where ((entity.Color == color) || (entity.Color.IsByLayer && layerIds.Contains(entity.LayerId)))
                                       select entity.Id;
                    
                    var entityNotIds = entityIds.Except(entityYesIds);
                    

                    switch (displayOrHide)
                    {
                        case DisplayOrHide.Display:
                            //未实现
                            break;
                        case DisplayOrHide.Hide:
                            foreach (var id in entityYesIds)
                                (trans.GetObject(id, OpenMode.ForWrite) as Entity).Visible = false;
                            break;
                        case DisplayOrHide.Isolate:
                            foreach (var id in entityNotIds)
                                (trans.GetObject(id, OpenMode.ForWrite) as Entity).Visible = false;
                            break;
                        default:
                            break;
                    }

                    trans.Commit();
                }
                catch (System.Exception ex)
                {
                    Application.ShowAlertDialog(ex.ToString());
                    trans.Abort();
                }
            }
        }
    }
}
